<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\LinkageRequest;
use App\Models\Linkage;
use App\Services\LinkageService;
use App\Traits\Admin\SuccessOrFailure;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Http\Request;

class LinkageController extends Controller
{
    use SuccessOrFailure;

    private $service;

    public function __construct(LinkageService $service)
    {
        $this->service = $service;
    }

    public function index($category = 'region', $pid = 0)
    {
//        $this->canOrFailure('common.linkage');
        $category_conf = config('setting.linkage_categories');
        $condition = [];
        $condition['category'] = $category_conf[$category];
        $condition['pid'] = $pid;
        $linkages = $this->service->all($condition);
        return view('admin.linkage.index', compact('linkages', 'category_conf', 'category', 'pid'));
    }

    public function create($category = 'region', $pid = 0)
    {
        $p_linkage = $this->service->getById($pid);
        return view('admin.linkage.create', compact('p_linkage'));
    }

    public function store(LinkageRequest $request)
    {
        $value = empty($request->get('value')) ? $request->get('name') : $request->get('value');
        $attributes = [
            'name' => $request->get('name'),
            'value' => $value,
            'category' => $request->get('category'),
            'pid' => $request->get('pid'),
            'extend' => $request->get('extend'),
            'sort' => $request->get('sort'),
        ];
        $this->service->create($attributes);
        $category_name = $request->get('category') == 2 ? 'carmodel' : 'region';
        $pid = $request->get('pid') ?? 0;
        return $this->success('', route('admin.linkage.index', ['category' => $category_name, 'pid' => $pid]));
    }


    public function edit($id)
    {
        $this->canOrFailure('common.linkage');
        $linkage = $this->getLinkageOrFailure($id);
        return view('admin.linkage.edit', compact('linkage'));
    }

    public function update(LinkageRequest $request, $id)
    {
        $this->canOrFailure('common.linkage');
        $linkage = $this->getLinkageOrFailure($id);
        $attributes = [
            'name' => $request->get('name'),
            'value' => $request->get('value'),
            'category' => $request->get('category'),
            'extend' => $request->get('extend'),
            'sort' => $request->get('sort'),
        ];
        $this->service->update($linkage, $attributes);
        return $this->success('', $request->get('back_url') ?: route('admin.linkage.index'));
    }

    public function destroy($id)
    {
        $this->canOrFailure('common.linkage');
        $linkage = $this->getLinkageOrFailure($id);
        $this->service->destroy($linkage);
        return $this->success();
    }

    public function cache()
    {
        $this->service->cache();
        return $this->success();
    }

    public function get_cache()
    {
        return $this->service->getLinkagesFromCache();
    }

    private function getLinkageOrFailure($id)
    {
        $linkage = $this->service->getById($id);
        if (!$linkage) {
            response($this->failure('值不存在！'), 404)->send();
            exit;
        }
        return $linkage;
    }
}
